package vinl.me.hibernate;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;

import vinl.me.model.User;

public class HibernateFactory {

	private static final String CONFIG_PATH = "./hibernate.cfg.xml";
	
	public static SessionFactory sessionFactory;
	
	public static Session session;
	
	static {
		try {
			Configuration config = new Configuration();
			sessionFactory = config.configure(CONFIG_PATH).buildSessionFactory();
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}
	
	public static SessionFactory sessionFactory() {
			return sessionFactory;
	}
	
	public static void main(String[] args) {
		Transaction tx = null;
		try {
			Query query = session.createQuery("from User");
			List list = query.list();
			for (int i = 0; i < list.size(); i++) {
				User user = (User)list.get(i);
				System.out.println(user.getId());
				System.out.println(user.getUserName());
				System.out.println(user.getPassword());
				System.out.println(user.getRole());
				System.out.println(user.getEmail());
				
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}
	
}
